package cn.edu.njpi.controller;

import cn.edu.njpi.model.TSchool;
import cn.edu.njpi.model.TUser;
import cn.edu.njpi.service.TSchoolService;
import cn.edu.njpi.utils.InformUtils;
import cn.edu.njpi.utils.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 朱宗辉
 * @version V1.0
 * @Package cn.edu.njpi.controller
 * @date 2022/4/20 2:08 PM
 */
@RestController
@RequestMapping(value = "/api/school")
public class SchoolController {
    @Autowired
    private TSchoolService tSchoolService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public T addSchool(@RequestBody TSchool tSchool, @RequestHeader("token") String token) {
        T t = new T();
        TUser tUser = (TUser) InformUtils.map.get(token);
        if (tUser != null && tUser.getUserPermssionId() == 1) {
            t = tSchoolService.addSchool(tSchool);
        } else {
            t.add("error", "错误，您未登录或权限不足！");
            t.setStatus(403);
        }
        return t;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public T deleteSchool(int schoolId, @RequestHeader("token") String token) {
        T t = new T();
        TUser tUser = (TUser) InformUtils.map.get(token);
        if (tUser != null && tUser.getUserPermssionId() == 1) {
            t = tSchoolService.deleteSchool(schoolId);
        } else {
            t.add("error", "错误，您未登录或权限不足！");
            t.setStatus(403);
        }
        return t;
    }

    @RequestMapping(value = "/change", method = RequestMethod.POST)
    public T changeSchool(@RequestBody TSchool tSchool, @RequestHeader("token") String token) {
        T t = new T();
        TUser tUser = (TUser) InformUtils.map.get(token);
        if (tUser != null && tUser.getUserPermssionId() == 1) {
            t = tSchoolService.changeSchool(tSchool);
        } else {
            t.add("error", "错误，您未登录或权限不足！");
            t.setStatus(403);
        }
        return t;
    }
}
